<?php
/**
* @author Amasty Team
* @copyright Amasty
* @package Amasty_Flags
*/
class Amasty_Flags_Adminhtml_FlagController extends Mage_Adminhtml_Controller_Action
{
    protected function _initAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('sales/amflags')
            ->_addBreadcrumb(Mage::helper('amflags')->__('Sales'),       Mage::helper('amflags')->__('Sales'))
            ->_addBreadcrumb(Mage::helper('amflags')->__('Order Flags'), Mage::helper('amflags')->__('Order Flags'))
        ;
        return $this;
    }
    
    public function indexAction()
    {
        $this->_title($this->__('Order Flags'));
             
        $this->_initAction()
            ->_addContent($this->getLayout()->createBlock('amflags/adminhtml_flag'))
            ->renderLayout();
    }
    
    public function newAction()
    {
        // the same form is used to create and edit
        $this->_forward('edit');
    }
    
    public function editAction()
    {
        $this->_title($this->__('Order Flags'))->_title($this->__('Edit Flag'));
        
        $id   = $this->getRequest()->getParam('flag_id');
        $flag = Mage::getModel('amflags/flag');
        if ($id) 
        {
            $flag->load($id);
            if (!$flag->getId()) 
            {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('amflags')->__('This flag no longer exists.'));
                $this->_redirect('*/*/');
                return;
            }
        }
        
        // Set entered data if was error when we do save
        $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
        if (! empty($data)) 
        {
            $flag->setData($data);
        }
        
        Mage::register('amflags_flag', $flag);
             
        $this->_initAction()
            ->_addContent($this->getLayout()->createBlock('amflags/adminhtml_flag_edit'))
            ->_addLeft($this->getLayout()->createBlock('amflags/adminhtml_flag_edit_tabs'))
            ->renderLayout();
    }
    
    public function saveAction()
    {
        if ($data = $this->getRequest()->getPost()) 
        {
            $flagId = $this->getRequest()->getParam('flag_id');
            $model  = Mage::getModel('amflags/flag');
            if ($flagId) 
            {
                $model->load($flagId);
            }
            $model->setData($data);
            try 
            {
                $model->save();
                $flagId = $model->getId();
                
                if (isset($_FILES['flag_image']['error']) && UPLOAD_ERR_OK == $_FILES['flag_image']['error'])
                {
                    // trying to upload image
                    $uploader = new Varien_File_Uploader('flag_image');
                    $uploader->setFilesDispersion(false);
                    $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                    $uploader->save(Amasty_Flags_Model_Flag::getUploadDir(), $model->getId() . '.jpg');
                }
                
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('amflags')->__('The flag has been saved.'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                
                $this->_redirect('*/*/');
                return;
                
            } catch (Exception $e) 
            {
                $this->_getSession()->addException($e, Mage::helper('amflags')->__('An error occurred while saving the flag: ') . $e->getMessage());
            }
            
            $this->_getSession()->setFormData($data);
            $this->_redirect('*/*/edit', array('flag_id' => $flagId));
            return;
        }
        $this->_redirect('*/*/');
    }
    
    public function deleteAction()
    {
        if ($id = $this->getRequest()->getParam('flag_id')) 
        {
            try 
            {
                $model = Mage::getModel('amflags/flag');
                $model->load($id);
                $model->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('amflags')->__('The flag has been deleted.'));
                $this->_redirect('*/*/');
                return;
                
            } catch (Exception $e) 
            {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('flag_id' => $id));
                return;
            }
        }
    }

    public function setFlagAction()
    {
        $orderId = $this->getRequest()->getParam('orderId');
        $flagId  = $this->getRequest()->getParam('flagId');
        $comment = $this->getRequest()->getParam('comment');
        if ($orderId)
        {
            try
            {
                $orderFlag = Mage::getModel('amflags/order_flag')->load($orderId, 'order_id');
                if (0 == $flagId)
                {
                    // removing flag
                    $orderFlag->delete();
                } else 
                {
                    $orderFlag->setOrderId($orderId);
                    $orderFlag->setFlagId($flagId);
                    $orderFlag->setComment($comment);
                    $orderFlag->save();
                }
            } catch (Exception $e)
            {
                $this->_getSession()->addException($e, Mage::helper('amflags')->__('An error occurred while setting order flag.'));
            }
        }
        return true;
    }
}
